﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Logger;
using Company.Shell.Interfaces;
using System.Reflection;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        private object? content;
        /// <summary>
        /// 页面
        /// </summary>
        public object? Content
        {
            get => content;
            set => SetProperty(ref content, value);
        }
        
        public ICommand? OpenViewCommand { get; }
        public ICommand? ExitCommand { get; }
        public ISession Session { get; }
        
        public MainViewModel(ISession session)
        {
            Session = session;
            OpenViewCommand = new RelayCommand<string>(OnOpenViewCommand);
            ExitCommand = new RelayCommand(OnExitCommand);
            Content = App.Current.Services.GetService(typeof(Views.IndexView));
            
            // 记录系统启动日志
            Logs.Info($"用户 [{Session.CurrentUser.UserName}] 进入主界面");
        }

        private void OnExitCommand()
        {
            var currentUser = Session.CurrentUser;
            
            // 记录退出系统操作日志
            Logs.Info($"用户 [{currentUser.UserName}] 请求退出系统");
            
            var result = HandyControl.Controls.MessageBox.Show("是否退出系统？",
                                                                         "提示",
                                                                         System.Windows.MessageBoxButton.YesNo,
                                                                         System.Windows.MessageBoxImage.Question);
            if (result == System.Windows.MessageBoxResult.Yes)
            {
                // 记录用户确认退出日志
                Logs.Info($"用户 [{currentUser.UserName}] 确认退出系统，系统即将关闭");
                
                try
                {
                    App.Current.Shutdown();
                }
                catch (Exception ex)
                {
                    // 记录退出异常日志
                    Logs.Error($"系统退出异常：{ex.Message}");
                    Logs.Error(ex);
                }
            }
            else
            {
                // 记录用户取消退出日志
                Logs.Info($"用户 [{currentUser.UserName}] 取消退出系统操作");
                return;
            }
        }

        private void OnOpenViewCommand(string? viewName)
        {
            try
            {
                var currentUser = Session.CurrentUser;
                
                Assembly assembly = this.GetType().Assembly;//获取当前程序集
                string fullTypeName = $"{assembly.GetName().Name}.Views.{viewName}";
                Type? viewType = assembly.GetType(fullTypeName);
                
                if (viewType != null)
                {
                    Content = App.Current.Services.GetService(viewType);
                    
                    // 记录页面切换日志
                    Logs.Info($"用户 [{currentUser.UserName}] 切换到页面：{viewName}");
                }
                else
                {
                    Content = null;
                    
                    // 记录页面未找到日志
                    Logs.Error($"用户 [{currentUser.UserName}] 尝试访问不存在的页面：{viewName}");
                }
            }
            catch (Exception ex)
            {
                // 记录页面切换异常日志
                Logs.Error($"页面切换异常，目标页面：{viewName}，异常信息：{ex.Message}");
                Logs.Error(ex);
            }
        }
    }
}
